package com.sense.kukoo.biz_explore.adapter.provider

import android.view.View
import android.widget.TextView
import com.alibaba.android.arouter.utils.TextUtils
import com.sense.kukoo.biz_explore.adapter.ExploreContentAdapter
import com.sense.kukoo.common.R
import com.sense.kukoo.common.repo.bean.BookBean
import com.sense.kukoo.common.repo.bean.ExploreContentBean
import com.sense.kukoo.common.utils.ApplicationContext
import com.sense.kukoo.common.wiget.EditorPicksItemView
import com.sense.kukoo.common.wiget.adapter.provider.MultiItemProvider
import com.sense.kukoo.common.wiget.adapter.viewholder.BaseViewHolder

class EditorPicks1ItemProvider(val mListener: ExploreContentAdapter.ExploreContentEventListener) :
    MultiItemProvider<ExploreContentBean>() {

    override fun getItemType(): Int = ExploreContentBean.TYPE_2_X_3

    override fun getItemLayoutId(): Int = R.layout.common_editor_picks_1

    override fun onBindViewHolder(viewHolder: BaseViewHolder, bean: ExploreContentBean) {
        val tv_see_all = viewHolder.getView(R.id.tv_see_all)
        if (bean.code==null) {
            tv_see_all.visibility = View.GONE
        }else{
            tv_see_all.visibility = View.VISIBLE
        }
        tv_see_all.setOnClickListener {
            mListener.onClickSeeAll(bean, bean.code!!)
        }
        //
        val tv_title = viewHolder.getView(R.id.tv_title) as TextView
        tv_title.text = bean.title

        val items: Array<EditorPicksItemView> = arrayOf(
                viewHolder.getView(R.id.item_1) as EditorPicksItemView,
                viewHolder.getView(R.id.item_2) as EditorPicksItemView,
                viewHolder.getView(R.id.item_3) as EditorPicksItemView,
                viewHolder.getView(R.id.item_4) as EditorPicksItemView,
                viewHolder.getView(R.id.item_5) as EditorPicksItemView,
                viewHolder.getView(R.id.item_6) as EditorPicksItemView
        )
        val books = bean.books!!
        items.forEachIndexed{ index, view ->
            if(books.size > index){
                val bookBean = books[index]
                when (bookBean.type) {
                    BookBean.TYPE_UNKNOW -> {
                        view.setTypeStr(ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.explore_type_unknow))
                    }
                    BookBean.TYPE_AUDIOBOOK -> {
                        view.setTypeStr(ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.explore_type_audiobook))
                    }
                    BookBean.TYPE_PODCAST -> {
                        view.setTypeStr(ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.explore_type_podcast))
                    }
                    BookBean.TYPE_SHORT_AUDIO -> {
                        view.setTypeStr(ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.explore_type_short_audio))
                    }
                    else -> {}
                }

                val name = if (TextUtils.isEmpty(bookBean.alias)) bookBean.name else bookBean.alias
                view.setDesStr(name)
                if(bookBean.isChapter == 1){
                    //单集
                    view.setIsMsgSubType(false)
                    view.setMsgStr("${bookBean.listeners ?: 0}")
                }else{
                    view.setIsMsgSubType(true)
                    view.setMsgStr("${bookBean.subscribers ?: 0}")
                }

                view.setIconUrl(bookBean.coverUrl)
                //播放事件
                view.setPlayBtnListener {
                    mListener.onClickBookPlay(bean, bookBean)
                }
                //点击事件
                view.setItemClickListener {
                    mListener.onClickBook(bean, bookBean)
                }
                view.visibility = View.VISIBLE
            }else{
                view.visibility = View.GONE
            }
        }

    }

}